package com.i2bt.util;

import com.i2bt.common.Contract;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.List;

/**
 * 
 * Title : 访问http工具类
 *
 * Description : 包装方便与访问http的post和get的方法
 *
 * CreatedTime : 2014-8-8
 *
 * @author wuliao & kakaxi & yi
 *
 * @version 1.0
 *
 */

public class HttpUtil {
	
	private static HttpClient httpClientStatic = new DefaultHttpClient();		//访问http的浏览器
	
	private HttpClient httpClient = new DefaultHttpClient();
	
	/**
	 * 重置http的浏览器
	 */
	public static void resetHttpClient() {
		httpClientStatic = new DefaultHttpClient();
	}
	
	/**
	 * 带json去post对应的url网站, 并返回响应字符串
	 * 这个方法是成员方法, 不是静态, 解决了同时发送post冲突问题
	 * @param url 网址
	 * @param param json串
	 * @return 返回响应字符串
	 * @throws IOException
	 */
	public String postAndResponseAsyn(String url, String param) throws IOException {
		HttpPost post = new HttpPost(url);
		HttpConnectionParams.setConnectionTimeout(post.getParams(), 
				Contract.CONNECT_TIMEOUT);
		post.getParams().setParameter("charset", "UTF-8");
		try {
			//设置post参数json
			post.setEntity(new StringEntity(param));
			HttpResponse response = httpClient.execute(post);
			return getResponse(response);
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		} finally {
			post.abort();
		}
	}
	
	/**
	 * 带参数组去post对应的url网站, 并返回响应字符串
	 * @param url 网址
	 * @param params 参数组 
	 * @return 返回响应字符串
	 * @throws IOException
	 */
	public static String postAndResponse(String url, List<NameValuePair> params) throws IOException {
		//新建post
		HttpPost post = new HttpPost(url);
		HttpConnectionParams.setConnectionTimeout(post.getParams(), 
				Contract.CONNECT_TIMEOUT);
		try {
			//设置post参数组
			post.setEntity(new UrlEncodedFormEntity(params));
			//访问url并返回响应对象
			HttpResponse response = httpClientStatic.execute(post);
			return getResponse(response);
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		} finally {
			post.abort();
		}
	}
	
	/**
	 * 带json去post对应的url网站, 并返回响应字符串
	 * @param url 网址
	 * @param param json串
	 * @return 返回响应字符串
	 * @throws IOException
	 */
	public static String postAndResponse(String url, String param) throws IOException {
		HttpPost post = new HttpPost(url);
		HttpConnectionParams.setConnectionTimeout(post.getParams(), 
				Contract.CONNECT_TIMEOUT);
		post.getParams().setParameter("charset", "UTF-8");
		try {
			//设置post参数json
			post.setEntity(new StringEntity(param));
			HttpResponse response = httpClientStatic.execute(post);
			return getResponse(response);
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		} finally {
			post.abort();
		}
	}
	
	public static String newPostAndResponse(String url, String param) throws IOException {
		HttpClient httpClient = new DefaultHttpClient();
		HttpPost post = new HttpPost(url);
		HttpConnectionParams.setConnectionTimeout(post.getParams(), 
				Contract.CONNECT_TIMEOUT);
		try {
			//设置post参数json
			post.setEntity(new StringEntity(param, "UTF-8"));
			HttpResponse response = httpClient.execute(post);
			return getResponse(response);
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		} finally {
			post.abort();
		}
	}
	
	/**
	 * 带参数组去post对应的url网站
	 * @param url 网址
	 * @param params 参数组 
	 * @throws IOException
	 */
	public static void post(String url, List<NameValuePair> params) throws IOException {
		HttpPost post = new HttpPost(url);
		HttpConnectionParams.setConnectionTimeout(post.getParams(), 
				Contract.CONNECT_TIMEOUT);
		try {
			post.setEntity(new UrlEncodedFormEntity(params));
			httpClientStatic.execute(post);
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		} finally {
			post.abort();
		}
	}
	
	/**
	 * 带json去post对应的url网站, 并返回响应字符串
	 * @param url 网址
	 * @param param json串
	 * @return 返回响应字符串
	 * @throws IOException
	 */
	public static void post(String url, String param) throws IOException {
		HttpPost post = new HttpPost(url);
		HttpConnectionParams.setConnectionTimeout(post.getParams(), 
				Contract.CONNECT_TIMEOUT);
		try {
			post.setEntity(new StringEntity(param));
			httpClientStatic.execute(post);
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		} finally {
			post.abort();
		}
	}
	
	/**
	 * get对应的url网址
	 * @param url
	 * @throws IOException
	 */
	public static void get(String url) throws IOException {
		HttpGet get = new HttpGet(url);
		HttpConnectionParams.setConnectionTimeout(get.getParams(), 
				Contract.CONNECT_TIMEOUT);
		try {
		    httpClientStatic.execute(get);
		} catch (IOException e) {
			e.printStackTrace();
			throw e;
		} finally {
			get.abort();
		}
	}
	
	/**
	 * 返回响应字符串
	 * @param reponse 响应类
	 * @return
	 */
	private static String getResponse(HttpResponse reponse) {
		String responseStr = null;
			try {
				responseStr = EntityUtils.toString(reponse.getEntity());
			} catch (ParseException e) {
				e.printStackTrace();
			} catch (IOException e) {
				e.printStackTrace();
			}
		
		return responseStr;
	}

}
